package com.mytijian.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.mytijian.openapi.adapter.alihalt.exception.AlihaltException;
import com.mytijian.openapi.core.exception.OpenClientNetException;
import com.mytijian.runtime.ServiceExecutorFacade;

@RestController
public class AliHealthController {
	
	@Autowired
	private ServiceExecutorFacade serviceExecutorFacade;

	@RequestMapping(value = "/adapters/alihealth")
	public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		try {
			serviceExecutorFacade.executeAliHealthService(request, response);
		} catch (AlihaltException e) {
			Map<String, String> map = new HashMap<>();
			map.put("responseCode", e.getResponseCode());
			map.put("responseMessage", e.getResponseMessage());
			response.getWriter().write(JSON.toJSONString(map));
		} catch (OpenClientNetException e) {
			Map<String, String> map = new HashMap<>();
			map.put("responseCode", "500");
			map.put("responseMessage", "网络异常");
			response.getWriter().write(JSON.toJSONString(map));
		} catch (Throwable t) {
			Map<String, String> map = new HashMap<>();
			map.put("responseCode", "500");
			map.put("responseMessage", "未知异常");
			response.getWriter().write(JSON.toJSONString(map));
		}
	}
}
